package com.novak.rss.reader;

import com.novak.rss.reader.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class RSSProcessActivity extends ListActivity
{
  public final static int TASK_LOOP_COMPLETE = 0;
  private ProgressDialog pd;
  private TextView mText;
  private List<NewsItem> li = new ArrayList<NewsItem>();
  private String path;
  private MyHandler handler = null;
  private void startProgress()
  {
    pd = ProgressDialog.show(RSSProcessActivity.this, getResources()
        .getString(R.string.waiting), "...", true);
  }

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newslist);
    mText = (TextView) findViewById(R.id.myText);
    Intent intent = this.getIntent();
    Bundle bunde = intent.getExtras();
    mText.setText(bunde.getString("title"));
    path = bunde.getString("path");
    handler = new MyHandler();
    startProgress();
    new Thread()
    {
      public void run()
      {
        try
        {
          init(path);
        } catch (Exception e)
        {
          handleException(e);
        } finally
        {
          handler.sendEmptyMessage(TASK_LOOP_COMPLETE);
        }
      }
    }.start();
    
  }

  private void handleException(Exception e)
  {
    Intent intent = new Intent();
    Bundle bundle = new Bundle();
    bundle.putString("error", "" + e);
    intent.putExtras(bundle);
    RSSProcessActivity.this.setResult(-1, intent);
    RSSProcessActivity.this.finish();
    return;
  }

  private void init(String path) throws ParserConfigurationException,
      SAXException, IOException
  {
    RSSHelper helper = null;
    helper = new RSSHelper(path);
   // title = helper.getCi().getTitle();
      li = helper.getItems();
  }

  private void buildUI()
  {    
    setListAdapter(new NewsListAdapter(this, li));
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position,
      long id)
  {
    NewsItem ns = (NewsItem) li.get(position);
    Intent intent = new Intent();
    intent.setClass(RSSProcessActivity.this, ShowFeedActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("title", ns.getTitle());
    bundle.putString("description", ns.getDesc());
    bundle.putString("url", ns.getLink());
    intent.putExtras(bundle);
    startActivityForResult(intent, 0);
  }

  public boolean onCreateOptionsMenu(Menu menu)
  {
    menu
        .addSubMenu(0, 2, 0, getResources().getString(R.string.refresh));
    menu.addSubMenu(0, 3, 0, getResources().getString(
        R.string.backwards));
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId())
    {
      case 2:
        startProgress();
        new Thread()
        {
          public void run()
          {
         
            try
            {
              init(path);
            } catch (Exception e)
            {
              handleException(e);
            } finally
            {
              handler.sendEmptyMessage(TASK_LOOP_COMPLETE);
            }
          }
        }.start();
          return true;
      case 3:
        setResult(0);
        finish();
        return true;
    }
    return false;
  }

  private class MyHandler extends Handler
  {
    public void handleMessage(Message msg)
    {
      switch (msg.arg1)
      {
        case TASK_LOOP_COMPLETE:
          pd.dismiss();
          buildUI();
          break;
      }
    }
  }

}
